//
//  CompanyViewController.m
//  kr_36
//
//  Created by lmt on 16/1/27.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CompanyViewController.h"
#import "CompanyTableViewCell.h"
#import "CompanyModel.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import "LMTAFNetwork.h"
#import "CompanyDetailViewController.h"

@interface CompanyViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataArr;

@property (nonatomic, assign) NSInteger i;

@end

@implementation CompanyViewController

- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"创业公司";
    
    _i = 2;
    
    // 创建tableView
    [self createTableView];
    
    // 数据解析
    [self dataHander];
    
    // 上拉加载
    [self loading];
}

#pragma mark - 上拉加载
- (void)loading
{
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
       
        [LMTAFNetwork getUrl:[NSString stringWithFormat:@"https://rong.36kr.com/api/mobi/filter/company?appver=3.2&pageSize=20&page=%ld&comType=fr&sortType=auto", _i] body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
            
            NSArray *arr = resposeObject[@"data"][@"data"];
            NSMutableArray *temp = [CompanyModel modelHanderWithArray:arr];
            for (CompanyModel *m in temp) {
                [self.dataArr addObject:m];
            }
            _i++;
            [_tableView reloadData];
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        [_tableView.mj_footer endRefreshing];
    }];
    _tableView.mj_footer = footer;
}

#pragma mark - 数据解析
- (void)dataHander
{
    NSString *str = @"https://rong.36kr.com/api/mobi/filter/company?appver=3.2&pageSize=20&page=1&comType=fr&sortType=auto";
    [LMTAFNetwork getUrl:str body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
       
        NSArray *arr = resposeObject[@"data"][@"data"];
        self.dataArr = [CompanyModel modelHanderWithArray:arr];
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    [_tableView registerClass:[CompanyTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CompanyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    CompanyModel *m = _dataArr[indexPath.row];
    [cell.logo sd_setImageWithURL:[NSURL URLWithString:m.logo]];
    cell.name.text = m.name;
    cell.industry.text = m.industry;
    cell.target.text = m.financeTarget;
    cell.brief.text = m.brief;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CompanyDetailViewController *cdVC = [[CompanyDetailViewController alloc] init];
    
    CompanyModel *m = _dataArr[indexPath.row];
    
    cdVC.cid = m.cid;
    
    [self.navigationController pushViewController:cdVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
